﻿<DataGrid x:Class="WpfRcp.Controls.PropertiesViewer"
          x:Name="thisControl"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:Controls="clr-namespace:WpfRcp.Controls" HeadersVisibility="None" CanUserAddRows="False"
          CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeRows="False" SelectionUnit="FullRow"
          AutoGenerateColumns="False"
          Loaded="OnPropertyViewerLoaded">
    <DataGrid.Resources>
        <DataTemplate DataType="{x:Type Controls:EnumContainer}">
            <TextBlock Text="{Binding Label}" />
        </DataTemplate>

        <DataTemplate DataType="{x:Type Controls:ValueContainer}">
            <TextBlock Text="{Binding Label}" />
        </DataTemplate>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTemplateColumn IsReadOnly="True" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid x:Name="m_Grid">
                        <TextBlock x:Name="m_Name" Text="{Binding Name}" />
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsSeparator}" Value="True">
                            <Setter TargetName="m_Grid" Property="Background" Value="#55FFBA00" />
                            <Setter TargetName="m_Name" Property="FontWeight" Value="Bold" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid x:Name="m_Grid">
                        <TextBlock x:Name="m_ReadOnlyString" Text="{Binding StringValue}" Visibility="Collapsed" />
                        <CheckBox x:Name="m_ReadOnlyBool" IsChecked="{Binding BoolValue}" Visibility="Collapsed"
                                IsEnabled="False" />
                        <TextBlock x:Name="m_ReadOnlyInt" Text="{Binding StringValue}" Visibility="Collapsed" />
                        <ComboBox x:Name="m_ReadOnlyEnum" SelectedValue="{Binding EnumValue}" IsEnabled="False"
                                ItemsSource="{Binding EnumValues}" SelectedValuePath="Value" Visibility="Collapsed" />
                        <ComboBox x:Name="m_ReadOnlyValue" SelectedValue="{Binding ObjectValue}" IsEnabled="False"
                                ItemsSource="{Binding Values}" SelectedValuePath="Value" Visibility="Collapsed" />
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding PropertyType}" Value="String">
                            <Setter TargetName="m_ReadOnlyString" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Bool">
                            <Setter TargetName="m_ReadOnlyBool" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Int">
                            <Setter TargetName="m_ReadOnlyInt" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Enum">
                            <Setter TargetName="m_ReadOnlyEnum" Property="Visibility" Value="Visible" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding PropertyType}" Value="Object">
                            <Setter TargetName="m_ReadOnlyValue" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding IsMultiLineString}" Value="True">
                            <Setter TargetName="m_ReadOnlyString" Property="TextWrapping" Value="WrapWithOverflow" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding IsSeparator}" Value="True">
                            <Setter TargetName="m_Grid" Property="Background" Value="#55FFBA00" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid x:Name="m_EditableGrid" Visibility="Collapsed">
                            <TextBox x:Name="m_EditString" Text="{Binding StringValue}" Visibility="Collapsed" />
                            <CheckBox x:Name="m_EditBool" IsChecked="{Binding BoolValue}" Visibility="Collapsed" />
                            <Controls:NumericSpinner x:Name="m_EditInt" Value="{Binding IntValue}" Visibility="Collapsed"
                                    Height="20" />
                            <ComboBox x:Name="m_EditEnum" SelectedValue="{Binding EnumValue, UpdateSourceTrigger=PropertyChanged}"
                                    ItemsSource="{Binding EnumValues}" SelectedValuePath="Value" Visibility="Collapsed" />
                            <ComboBox x:Name="m_EditValue" SelectedValue="{Binding ObjectValue, UpdateSourceTrigger=PropertyChanged}"
                                    ItemsSource="{Binding Values}" SelectedValuePath="Value" Visibility="Collapsed" />
                        </Grid>
                        <Grid x:Name="m_ReadOnlyGrid" Visibility="Collapsed">
                            <TextBlock x:Name="m_ReadOnlyString" Text="{Binding StringValue}" Visibility="Collapsed" />
                            <CheckBox x:Name="m_ReadOnlyBool" IsChecked="{Binding BoolValue}" Visibility="Collapsed"
                                    IsEnabled="False" />
                            <TextBlock x:Name="m_ReadOnlyInt" Text="{Binding StringValue}" Visibility="Collapsed" />
                            <ComboBox x:Name="m_ReadOnlyEnum" SelectedValue="{Binding EnumValue, UpdateSourceTrigger=PropertyChanged}" IsEnabled="False"
                                    ItemsSource="{Binding EnumValues}" SelectedValuePath="Value" Visibility="Collapsed" />
                            <ComboBox x:Name="m_ReadOnlyValue" SelectedValue="{Binding ObjectValue, UpdateSourceTrigger=PropertyChanged}" IsEnabled="False"
                                    ItemsSource="{Binding Values}" SelectedValuePath="Value" Visibility="Collapsed" />
                        </Grid>
                    </Grid>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding PropertyType}" Value="String">
                            <Setter TargetName="m_EditString" Property="Visibility" Value="Visible" />
                            <Setter TargetName="m_ReadOnlyString" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Bool">
                            <Setter TargetName="m_EditBool" Property="Visibility" Value="Visible" />
                            <Setter TargetName="m_ReadOnlyBool" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Int">
                            <Setter TargetName="m_EditInt" Property="Visibility" Value="Visible" />
                            <Setter TargetName="m_ReadOnlyInt" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        
                        <DataTrigger Binding="{Binding PropertyType}" Value="Enum">
                            <Setter TargetName="m_EditEnum" Property="Visibility" Value="Visible" />
                            <Setter TargetName="m_ReadOnlyEnum" Property="Visibility" Value="Visible" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding PropertyType}" Value="Object">
                            <Setter TargetName="m_EditValue" Property="Visibility" Value="Visible" />
                            <Setter TargetName="m_ReadOnlyValue" Property="Visibility" Value="Visible" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding IsReadOnly}" Value="True">
                            <Setter TargetName="m_ReadOnlyGrid" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsReadOnly}" Value="False">
                            <Setter TargetName="m_EditableGrid" Property="Visibility" Value="Visible" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding IsMultiLineString}" Value="True">
                            <Setter TargetName="m_EditString" Property="TextWrapping" Value="WrapWithOverflow" />
                            <Setter TargetName="m_ReadOnlyString" Property="TextWrapping" Value="WrapWithOverflow" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
